package controler.states;
import controler.A_State;
import controler.A_UserCommand;
import controler.userCommands.AddToSelection;
import controler.userCommands.ChangeUserViewPoint;
import controler.userCommands.MoveElement;
import controler.userCommands.RemoveFromSelection;
import controler.userCommands.SwitchState;
import graphic.RenderObject;
import view.events.LevelEditorViewKeyboardEvent;
import view.events.LevelEditorViewMouseEvent;

/**
 * @author Damien Mabin
 */

class MultipleSelection extends A_State
{

	public function new() 
	{
		super();
		this.addEventListener(LevelEditorViewMouseEvent.EVENT, OnLevelEditorMouseEvent);
		this.addEventListener(LevelEditorViewKeyboardEvent.EVENT, OnLevelEditorKeyboardEvent);
	}
	
	private function OnLevelEditorKeyboardEvent(_Evt:LevelEditorViewKeyboardEvent):Void
	{
		switch(_Evt.m_Type)
		{
			case KEY_DOWN :
			{
			}
			case KEY_UP :
			{
				if (_Evt.m_KeyCode == 17 /*CTRL_KEY*/)
				{
					if (A_State.s_Model.VUE_GetSelection(RenderObject).length > 0)
					{
						A_State.s_Controller.DoUserCommand(new SwitchState(A_State.s_Controller.SIMPLE_SELECTION));
					}
					else
					{
						A_State.s_Controller.DoUserCommand(new SwitchState(A_State.s_Controller.INITIAL));
					}
				}
			}
		}
	}
	
	private function OnLevelEditorMouseEvent(_Evt:LevelEditorViewMouseEvent):Void
	{
		switch(_Evt.m_Type)
		{
			case MOUSE_DOWN :
			{
				switch(_Evt.m_TargetType)
				{
					case RENDER_OBJECT :
					{
						if(!A_State.s_Controller.m_KeyboardKeyState[32/*Space*/])
						{
							if (!A_State.s_Model.IsSelected(RenderObject, _Evt.m_Target))
							{
								A_State.s_Controller.DoUserCommand(new AddToSelection(null, RenderObject, _Evt.m_Target));
							}
							else if(A_State.s_Controller.m_KeyboardKeyState[16/*shift key code*/])
							{
								A_State.s_Controller.DoUserCommand(new RemoveFromSelection(null, RenderObject, _Evt.m_Target));
							}
							else
							{
								var cmd : A_UserCommand = new MoveElement(A_State.s_Controller.MOVE_SELECTION, RenderObject, cast _Evt.m_Target, 0, 0, 0);
								cmd.m_Stackable = false;
								A_State.s_Controller.DoUserCommand(cmd);
							}
						}
					}
					case SCENE :
					{
						
					}
					default :
					{
						
					}
				}
			}
			case MOUSE_MOVE :
			{
				if (A_State.s_Controller.m_KeyboardKeyState[32/*space*/])
				{
					A_State.s_Controller.DoUserCommand(new ChangeUserViewPoint(null, _Evt.m_MouseMovement.x, _Evt.m_MouseMovement.y, 0));
				}
			}
			default :
			{
				
			}
		}
	}
}